package com.pianzhu.blockchainemr.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.pianzhu.blockchainemr.mapper.AdminMapper;
import com.pianzhu.blockchainemr.pojo.*;
import com.pianzhu.blockchainemr.pojo.Param.OperationLogQuery;
import com.pianzhu.blockchainemr.pojo.result.PageResult;
import com.pianzhu.blockchainemr.pojo.result.RecentActivitiesResult;
import com.pianzhu.blockchainemr.pojo.result.StatisticsResult;
import com.pianzhu.blockchainemr.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class AdminServiceImpl implements AdminService {
    @Autowired
    private AdminMapper adminMapper;

    @Override
    public List<EmrDeptDict> getDeptList() {
        List<EmrDeptDict> deptList = adminMapper.getDeptList();
        return deptList;
    }

    @Override
    public void addDept(EmrDeptDict dept) {
        adminMapper.addDept(dept);
    }

    @Override
    public void updateDept(EmrDeptDict dept) {
        adminMapper.updateDept(dept);
    }

    @Override
    public void deleteDept(EmrDeptDict dept) {
        adminMapper.deleteDept(dept);
    }

    @Override
    public List<EmrDiseaseDict> getDiseaseList() {
        List<EmrDiseaseDict> diseaseList = adminMapper.getDiseaseList();
        return diseaseList;
    }

    @Override
    public void addDisease(EmrDiseaseDict disease) {
        adminMapper.addDisease(disease);
    }

    @Override
    public void updateDisease(EmrDiseaseDict disease) {
        adminMapper.updateDisease(disease);
    }

    @Override
    public void deleteDisease(EmrDiseaseDict disease) {
        adminMapper.deleteDisease(disease);
    }

    @Override
    public List<EmrExamItemDict> getExamItemList() {
        List<EmrExamItemDict> examItemList = adminMapper.getExamItemList();
        return examItemList;
    }

    @Override
    public void addExamItem(EmrExamItemDict examItem) {
        adminMapper.addExamItem(examItem);
    }

    @Override
    public void updateExamItem(EmrExamItemDict examItem) {
        adminMapper.updateExamItem(examItem);
    }

    @Override
    public void deleteExamItem(EmrExamItemDict examItem) {
        adminMapper.deleteExamItem(examItem);
    }

    @Override
    public List<EmrDrugDict> getDrugList() {
        List<EmrDrugDict> drugList = adminMapper.getDrugList();
        return drugList;
    }

    @Override
    public void addDrug(EmrDrugDict drug) {
        adminMapper.addDrug(drug);
    }

    @Override
    public void updateDrug(EmrDrugDict drug) {
        adminMapper.updateDrug(drug);
    }

    @Override
    public void deleteDrug(EmrDrugDict drug) {
        adminMapper.deleteDrug(drug);
    }

    @Override
    @Transactional
    public StatisticsResult getStatistics() {
        StatisticsResult statisticsResult = new StatisticsResult();
        statisticsResult.setDeptCount(adminMapper.getDeptList().size());
        statisticsResult.setDiseaseCount(adminMapper.getDiseaseList().size());
        statisticsResult.setExamItemCount(adminMapper.getExamItemList().size());
        statisticsResult.setDrugCount(adminMapper.getDrugList().size());
        statisticsResult.setUserCount(adminMapper.getUsersCount());
        return statisticsResult;
    }

    @Override
    public List<RecentActivitiesResult> getRecentActivitiesResult() {
        List<RecentActivitiesResult> recentActivitiesResultList = adminMapper.getRecentActivitiesResult();
        return recentActivitiesResultList;
    }

    @Override
    public PageResult<OperationLog> getOperationList(OperationLogQuery query) {
        // 设置分页参数
        PageHelper.startPage(query.getPage(), query.getSize());

        // 执行查询
        List<OperationLog> list = adminMapper.getOperationList(
                query.getModule(),
                query.getOperation(),
                query.getOperatorName(),
                query.getStatus()
        );

        // 获取分页信息
        PageInfo<OperationLog> pageInfo = new PageInfo<>(list);

        // 使用正确的构造函数 - 传入4个参数
        return new PageResult<>(
                pageInfo.getPageNum(),    // pageNum
                pageInfo.getPageSize(),   // pageSize
                pageInfo.getTotal(),      // total
                pageInfo.getList()        // list
        );
    }
}
